using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using YahooMusicEngine;
using YahooMusicEngine.OnlineDataProvider;
using YahooMusicEngine.Services;
using YahooMusicEngine.Entities;
using Yahoo;

namespace Test
{
  public partial class Form1 : Form
  {
    ServiceProvider provider = new ServiceProvider();
    CategoryTreeService catserv = new CategoryTreeService();
    CategoryEntity lastItems = new CategoryEntity();
    VideosForACategoryService videolist = new VideosForACategoryService();
    SearchForVideosService videosearch = new SearchForVideosService();
    
    public Form1()
    {
      InitializeComponent();
                     
      provider.AppId = "DeUZup_IkY7d17O2DzAMPoyxmc55_hTasA--";
      provider.SharedSecret = "d80b9a5766788713e1fadd73e752c7eb";
      provider.Token = textBox3.Text;
      provider.Init();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      catserv.Type = CategoryTreeTypes.genre;
      provider.GetData(catserv);
      refreshlist(catserv.Items);
    }

    void refreshlist(List<CategoryEntity> cats)
    {
      listView1.Items.Clear();
      foreach (CategoryEntity cat in cats)
      {
        ListViewItem item = new ListViewItem(cat.Name);
        item.Tag = cat;
        listView1.Items.Add(item);
      }
    }

    void addVideos(List<VideoResponse> vids)
    {
      foreach (VideoResponse vi in vids)
      {
        ListViewItem item = new ListViewItem(vi.Video.Title+"(V)");
        item.Tag = vi;
        listView1.Items.Add(item);
      }
    }

    private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
      if (listView1.SelectedItems.Count > 0)
      {
        CategoryEntity cats = listView1.SelectedItems[0].Tag as CategoryEntity;
        if (cats != null)
        {
          lastItems = cats;
          listView1.Items.Clear();
          if (cats.Childs.Count > 0)
          {
            refreshlist(cats.Childs);
          }
          if (cats.VideoCount > 0)
          {
            videolist.Count = cats.VideoCount;
            videolist.Category = cats.Id;
            provider.GetData(videolist);
            addVideos(videolist.Items);
          }
        }
        else
        {
          VideoResponse vid = listView1.SelectedItems[0].Tag as VideoResponse;
          if (vid != null)
          {
            VideoPlayer player = new VideoPlayer(vid.Video.Id);
            player.AutoStart = true;
            textBox1.Text = player.VideoPlayerUrl;
          }
        }
      }
    }

    private void button2_Click(object sender, EventArgs e)
    {
      //Category cats = ((Category)listView1.Items[0].Tag);
      if (lastItems.Parent != null)
      {
        refreshlist(lastItems.Parent.Childs);
      }
      else
      {
        refreshlist(catserv.Items);
      }
    }

    private void button3_Click(object sender, EventArgs e)
    {
      videosearch.Keyword = textBox_search.Text;
      provider.GetData(videosearch);
      addVideos(videosearch.Items);

    }

    private void button4_Click(object sender, EventArgs e)
    {
      // Create an instance of Yahoo.Authentication   
      Yahoo.Authentication auth = new Authentication("DeUZup_IkY7d17O2DzAMPoyxmc55_hTasA--", "d80b9a5766788713e1fadd73e752c7eb");
      // Redirect the user to the use sign-in page  
      webBrowser1.Url = auth.GetUserLogOnAddress();

    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
      string query = e.Url.Query.Substring(1); //remove the ?

      string[] pairs = query.Split(new char[] { '&' });
      foreach (string s in pairs)
      {
        string[] pair = s.Split(new char[] { '=' });
        if (pair[0]=="token")
        {
          MessageBox.Show(pair[1]);
        }
      }

      MessageBox.Show("Loading done !" + e.Url.ToString());
    }

    private void button5_Click(object sender, EventArgs e)
    {
      UserInformationService userinfo = new UserInformationService();
      provider.Token = textBox3.Text;
      provider.GetData(userinfo);
      if (string.IsNullOrEmpty(userinfo.Response.User.Ymid))
      {
        MessageBox.Show("Test fail, wrong token");
      }
      else
      {
        MessageBox.Show("Verification donne");
      }

      propertyGrid1.SelectedObject = userinfo;
    }

    private void button6_Click(object sender, EventArgs e)
    {
      UserInformationService userinfo = new UserInformationService();
      provider.GetData(userinfo);
      RecentlyPlayedVideosService recent = new RecentlyPlayedVideosService();
      recent.Ymid = userinfo.Response.User.Ymid;
      provider.GetData(recent);
      addVideos(recent.Items);
    }

    private void button7_Click(object sender, EventArgs e)
    {
      VideosForAGivenArtistLetterService sh = new VideosForAGivenArtistLetterService();
      sh.Letter = textBox2.Text;
      provider.GetData(sh);
      addVideos(sh.Items);
    }

    private void button8_Click(object sender, EventArgs e)
    {
      SimilarVideosService similar = new SimilarVideosService();
      similar.Id = textBox4.Text;
      provider.GetData(similar);
      addVideos(similar.Items);
    }

    private void button9_Click(object sender, EventArgs e)
    {
      //UserFavoriteVideosService fav = new UserFavoriteVideosService();
      //fav.Ymid = provider.CurentUser.Ymid;
      //fav.Ids = "60149411,157417610";
      //provider.PostData(fav);
      VideosForGivenPublishedListService ser = new VideosForGivenPublishedListService();
      ser.Id = "new";
      provider.GetData(ser);
    }
  }
}